<?php

 /**
  * @package JooS
  * @subpackage PHPackager
  */

  require_once "JooS/Helper/Abstract.php";

  class JooS_PHPackager_Helper_Copy_Withreplace extends JooS_Helper_Abstract {
    public function __invoke($source, $path, $replace) {
      require_once "JooS/Config.php";
      require_once "JooS/Event.php";
      require_once "JooS/PHPackager/FS.php";

      $Event_newFile = JooS_Event::newInstance(JooS_PHPackager_FS::EVENT_NEW_FILE)
        ->attach(function($realPath, $path, JooS_CodeGenerator_File_Adapter $tmpFile) use ($replace) {
          $contents = $tmpFile->file_get_contents();
          foreach ($replace as $k => $v) {
            $contents = str_replace($k, $v, $contents);
          }
          $tmpFile->file_put_contents($contents);
        })
        ->commit();

      JooS_PHPackager_FS::getInstance()->copy($source, $path);

      $Event_newFile->rollback();
    }
  }
